#include "aceMesh_stack.h"
#ifdef TARG_SW5
#include <share.h>
#endif
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#if defined(TARG_SW5) || defined(TARG_SW9)
#include <stdbool.h>
#endif


#if defined(EMP_QUEUE) && defined(EMP_CONCURRENT_Q)
#include "emp_basic.h"
#endif

/*
void my_mutex_lock(volatile int* my_lock)
{
#ifdef TARG_SW5
#ifdef MUTEX_BASIC
    unsigned int __addr,__tmpi;
    __asm__ __volatile__(
        "        memb\n"
        "        ldi      %1,%2\n"
        "1:      ldw_inc  %0,0(%1)\n"
        "        bne      %0,1b\n"
        "        memb\n"
      : "=&r"  (__tmpi),
        "=&r"  (__addr)
      : "m" (*my_lock)
      : "memory");
    return;
#else
    unsigned int __tmp = 0;
    unsigned int __cnt;
    __asm__ __volatile__ (
    "0:     ldw     %[__tmp], %[my_lock]\n"
    "       beq     %[__tmp], 2f\n"
    "       ldi     %[__cnt], 50\n"
    "       sll     %[__cnt], 4, %[__cnt]\n"
    "1:     subw    %[__cnt], 1, %[__cnt]\n"
    "       bne     %[__cnt], 1b\n"
    "       br      0b\n"
    "2:     ldw_inc    %[__tmp], %[my_lock]\n"
    "       bne     %[__tmp], 0b\n"
    "       memb    \n"
    "       br      3f\n"
    "3:     unop    \n"
    : [__tmp] "=&r"(__tmp), [__cnt] "=&r"(__cnt)
    : [my_lock] "m"(*(my_lock))
    : "memory");
    return;
#endif
#elif TARG_SW9
    assert(0);
#endif
}
int my_mutex_unlock(volatile int* my_lock)
{
#ifdef TARG_SW5
#ifdef MUTEX_BASIC
    unsigned int __addr ;
    __asm__ __volatile__(
        "        memb\n"
        "        ldi      %0,%1\n"
        "        stw  $31,0(%0)\n"
        "        memb\n"
      : "=&r"  (__addr)
      : "m" (*my_lock)
      : "memory");
    return;
#else
    unsigned int __tmp = 0;
    __asm__ __volatile__ (
    "       memb    \n"
    "       mov     0, %[__tmp]\n"
    "       stw     %[__tmp], %[my_lock]\n"
    : [__tmp] "=&r"(__tmp)
    : [my_lock] "m"(*(my_lock))
    : "memory"
    );
    return;
#endif
#elif TARG_SW9
    assert(0);
#endif
}
*/

struct task* _sync_val_compare_and_swap(volatile int* my_lock
                       ,struct task  **mem
                       , struct task*  old
                       , struct task*  new)
{
#ifdef TARG_SW5
  unsigned int __addr,__cmp;
  unsigned int  __tmpi;
  struct task* __prev, *__tmpp;
  __asm__ __volatile__(
      "        memb\n"
      "        ldi      %3,%6\n"
      "1:      ldw_inc  %2,0(%3)\n"
      "        bne      %2,1b\n"
      "        memb\n"
      "        ldl      %0,%5\n"
      "        cmpeq    %0,%7,%1\n"
      "        beq      %1,2f\n"
      "        mov      %8,%4\n"
      "        stl      %4,%5\n"
      "        mov      1,%1\n"
      "2:      memb\n"
      "        mov      %0,%4\n"
      "        stw      $31,0(%3)\n"
      "        memb\n"
      : "=&r"  (__prev),
        "=&r"  (__cmp),
        "=&r"  (__tmpi),
        "=&r"  (__addr),
        "=&r"  (__tmpp)
      : "m" (*(mem)),
        "m" (*my_lock),
        "Ir" (old),
        "Ir" (new)
      : "memory");
    return __tmpp;
#elif TARG_SW9
    assert(0);
#endif
}


#if defined(TARG_SW5) && defined(EMP_QUEUE) && defined(EMP_CONCURRENT_Q)
#ifdef EMP_MASTER
    //有可能是插入主核队列，也有可能是插入从核队列
#else
    //只是插入从核队列
#endif




#ifdef EMP_MASTER
int EQ_queue_full(void * queue_ptr, int is_master)
#else
int EQ_queue_full(EQ_queue * queue)
#endif
{
    #ifdef EMP_MASTER
    if (is_master) {
        EQ_master_queue * queue = (EQ_master_queue *)queue_ptr;
        return (queue->tail - queue->head) == queue->size;
    }
    else {
        EQ_queue * queue = (EQ_queue *)queue_ptr;
        return (queue->tail - queue->head) == queue->size;
    }
    #else
    return (queue->tail - queue->head) == queue->size;
    #endif
}

#ifdef EMP_MASTER
int EQ_queue_empty(void * queue_ptr, int is_master)
#else
int EQ_queue_empty(EQ_queue * queue)
#endif
{
    #ifdef EMP_MASTER
    if (is_master) {
        EQ_master_queue * queue = (EQ_master_queue *)queue_ptr;
        return queue->tail == queue->head;
    }
    else {
        EQ_queue * queue = (EQ_queue *)queue_ptr;
        return queue->tail == queue->head;
    }
    #else
    return queue->tail == queue->head;
    #endif
}

unsigned EQ_queue_length(EQ_queue * queue)
{
    return queue->tail - queue->head;
}

#ifdef EMP_MASTER
unsigned EQ_queue_space(EQ_queue * queue)
{
    return queue->size - EQ_queue_length(queue);
}
#endif






#ifdef EMP_CONCURRENT_Q
//主核直接写入并发的写入任务队列
//两个队列，一个是master_queue，类型为EQ_master_queue*；一个是slave队列EQ_queue*
#ifdef EMP_MASTER
void EQ_queue_master_push(void * queue_ptr, struct task * t, int is_master)
#else
void EQ_queue_master_push(EQ_queue * queue, struct task * t)
#endif
{
    #ifdef EMP_MASTER
    if (is_master) {//插入主核队列
        EQ_master_queue * queue = (EQ_master_queue *)queue_ptr;
        my_mutex_lock(&(queue->my_q_lock)); 
        if (!EQ_queue_full(queue, 1)) {
            queue->array[queue->tail % queue->size] = t;
            queue->tail++;
        }
        else {
            printf("master queue full!!!!!!!\n");
        }
        my_mutex_unlock(&(queue->my_q_lock)); 
    }
    else {//插入从核队列
        EQ_queue * queue = (EQ_queue *)queue_ptr;
        my_mutex_lock(&(queue->my_q_lock)); 
        if (!EQ_queue_full(queue, 0)) {
            queue->array[queue->tail % queue->size] = t;
            queue->tail++;
        }
        else {
            printf("slave queue full!!!!!!!\n");
        }
        my_mutex_unlock(&(queue->my_q_lock)); 
    }

    #else //直接插入从核队列
    my_mutex_lock(&(queue->my_q_lock)); 
    if (!EQ_queue_full(queue)) {
        queue->array[queue->tail % queue->size] = t;
        queue->tail++;
    }
    else {
        printf("queue full!!!!!!!\n");
    }
    my_mutex_unlock(&(queue->my_q_lock)); 
    #endif


}

#ifdef EMP_MASTER
struct task * EQ_queue_master_pop(void * queue_ptr, int is_master)
#else
struct task * EQ_queue_master_pop(EQ_queue * queue)
#endif
{
    #ifdef EMP_MASTER
    if (is_master) {
        EQ_master_queue * queue = (EQ_master_queue *)queue_ptr;
        my_mutex_lock(&(queue->my_q_lock)); 
        if (!EQ_queue_empty(queue, 1)) {
            struct task * temp = queue->array[queue->head % queue->size];
            queue->head++;
            my_mutex_unlock(&(queue->my_q_lock)); 
            return temp;
        }
        else {
            my_mutex_unlock(&(queue->my_q_lock)); 
            return NULL;
        }
    }
    else {
        EQ_queue * queue = (EQ_queue *)queue_ptr;
        my_mutex_lock(&(queue->my_q_lock)); 
        if (!EQ_queue_empty(queue, 0)) {
            struct task * temp = queue->array[queue->head % queue->size];
            queue->head++;
            my_mutex_unlock(&(queue->my_q_lock)); 
            return temp;
        }
        else {
            my_mutex_unlock(&(queue->my_q_lock)); 
            return NULL;
        }
    }

    #else

    my_mutex_lock(&(queue->my_q_lock)); 
    if (!EQ_queue_empty(queue)) {
        struct task * temp = queue->array[queue->head % queue->size];
        queue->head++;
        my_mutex_unlock(&(queue->my_q_lock)); 
        return temp;
    }
    else {
        my_mutex_unlock(&(queue->my_q_lock)); 
        return NULL;
    }

    #endif


}
#endif

#endif






#ifdef TARG_SW9
void push(struct concurrent_aceMesh_stack* self,struct  task* t)
{
    struct task* old_top=NULL;
    old_top = self->task_pool;
    t->next = old_top;
    self->task_pool=t;
}
#ifdef _SERIAL_QUEUE
void push_to_slave(struct concurrent_aceMesh_stack* self,struct  task* t)
{
    struct task* old_top=NULL;
    old_top = self->task_pool;
    t->next = old_top;
    self->task_pool=t;
}
#endif
#if defined(MPI_SURPPORT) && defined(MASTER)
int try_pop(struct concurrent_aceMesh_stack* self,struct task** t)
{
    struct task* old_top = self->task_pool;
    if( old_top == NULL )
       return  false;
    else{
       *t = old_top;
       self->task_pool = old_top->next;
       return true;
   }  
}
#endif
#endif


#ifdef TARG_SW5
  #if defined( _SERIAL_QUEUE)
void push(struct concurrent_aceMesh_stack* self,struct task* t)    
{            
#ifdef mutex_lock
    struct task* old_top=NULL;
    my_mutex_lock(&(self->my_lock));
    old_top = self->task_pool;
    t->next=old_top;
    self->task_pool=t;
    my_mutex_unlock(&(self->my_lock));
#else
    struct task* old_top=NULL;
    do{
      old_top = self->task_pool;
      t->next = old_top;
    } while(_sync_val_compare_and_swap(&(self->my_lock),
                     &(self->task_pool),
                      old_top,t)!=old_top);
#endif
}

    #ifdef MASTER
int try_pop(struct concurrent_aceMesh_stack* self,struct task** t)
{
#ifdef mutex_lock    
    struct task* old_top =NULL;
    if(self->task_pool==NULL)
        return false;
    else
    {
        my_mutex_lock(&(self->my_lock));
        struct task* new_top=NULL;
        old_top = self->task_pool;
        new_top = old_top->next;
        
        *t=old_top;
        self->task_pool=new_top;
        my_mutex_unlock(&(self->my_lock));
        if((*t)==NULL)
        {
            return false;
        }
        else
        {
            return true;
       }
    } 
#else   
    struct task* old_top = self->task_pool;
    if( old_top == NULL )
    {
       return  false;
    }else{
       struct task* new_top = old_top->next;
       if(_sync_val_compare_and_swap(&(self->my_lock), &(self->task_pool),old_top,new_top)==old_top)
       {
          *t = old_top; 
          return true;
       }else{
          *t=NULL;
          return false;
       }
    } 
#endif
}
void push_to_slave(struct aceMesh_stack* self,struct  task* t)
{
    struct task* old_top=NULL;
    old_top = self->task_pool;
    t->next = old_top;
    self->task_pool=t;
}
    #endif
  #elif _CIRCULAR_QUEUE
    #ifdef MASTER
void push(struct concurrent_aceMesh_stack* self,struct task* t)    
{
#ifdef mutex_lock
    struct task* old_top=NULL;
    my_mutex_lock(&(self->my_lock));
    old_top = self->task_pool;
    t->next=old_top;
    self->task_pool=t;
    my_mutex_unlock(&(self->my_lock));
#else
    struct task* old_top=NULL;
    do{
      old_top = self->task_pool;
      t->next = old_top;
    } while(_sync_val_compare_and_swap(&(self->my_lock),
                     &(self->task_pool),
                      old_top,t)!=old_top);
#endif
}
int try_pop(struct concurrent_aceMesh_stack* self,struct task** t)
{
#ifdef mutex_lock    
    struct task* old_top =NULL;
    if(self->task_pool==NULL)
        return false;
    else
    {
        my_mutex_lock(&(self->my_lock));
        struct task* new_top=NULL;
        old_top = self->task_pool;
        new_top = old_top->next;
        
        *t=old_top;
        self->task_pool=new_top;
        my_mutex_unlock(&(self->my_lock));
        if((*t)==NULL)
        {
            return false;
        }
        else
        {
            return true;
       }
    } 
#else   
    struct task* old_top = self->task_pool;
    if( old_top == NULL )
    {
       return  false;
    }else{
       struct task* new_top = old_top->next;
       if(_sync_val_compare_and_swap(&(self->my_lock), &(self->task_pool),old_top,new_top)==old_top)
       {
          *t = old_top; 
          return true;
       }else{
          *t=NULL;
          return false;
       }
    } 
#endif    
}
void push_to_slave(struct aceMesh_stack* self,struct  task* t)
{		
    struct task** base=self->base;
	int curtop=self->top;
    base[curtop]=t;
    //printf("push %d to%d,",t->affinity_id,curtop);
    curtop=(curtop+1)%CIRCULAR_ARRAY_SIZE;
    if (curtop==self->bottom) {
      printf("sorry: one mqueue(circular) is full!\n"); //actually is safe currently
	  exit(1);
	}
    self->top=curtop;
}
    #else
void push(struct aceMesh_stack* self,struct  task* t)
{
    struct task* old_top=NULL;
    old_top = self->task_pool;
    t->next = old_top;
    self->task_pool=t;
}
    #endif
  #else

    #ifdef CENTRAL_FIFO
inline int queue_empty(struct concurrent_aceMesh_stack* self)
{
    return self->head_ptr == NULL;
}
    #endif
void init_push(struct concurrent_aceMesh_stack* self,struct task* t)
{
#ifdef CENTRAL_FIFO
    if (queue_empty(self)) {
        self->head_ptr = t;
        self->tail_ptr = t;
    }
    else {
        self->tail_ptr->next = t;
        self->tail_ptr = t;
    }
#else
    struct task* old_top=NULL;
    old_top = self->task_pool;
    t->next=old_top;
    self->task_pool=t;
#endif
}

void push(struct concurrent_aceMesh_stack* self,struct task* t)    
{
#ifdef mutex_lock
    #ifdef CENTRAL_FIFO
    
    my_mutex_lock(&(self->my_lock));
    if (queue_empty(self)) {
        self->head_ptr = t;
        self->tail_ptr = t;
    }
    else {
        self->tail_ptr->next = t;
        self->tail_ptr = t;
    }
    my_mutex_unlock(&(self->my_lock));


    #else

    struct task* old_top=NULL;
    my_mutex_lock(&(self->my_lock));
    old_top = self->task_pool;
    t->next=old_top;
    self->task_pool=t;
    my_mutex_unlock(&(self->my_lock));

    #endif
#else
    struct task* old_top=NULL;
    do{
      old_top = self->task_pool;
      t->next = old_top;
    } while(_sync_val_compare_and_swap(&(self->my_lock),
                     &(self->task_pool),
                      old_top,t)!=old_top);
#endif
}
int try_pop(struct concurrent_aceMesh_stack* self,struct task** t)
{
#ifdef mutex_lock    
    #ifdef CENTRAL_FIFO

    my_mutex_lock(&(self->my_lock));
    if (queue_empty(self)) {
        my_mutex_unlock(&(self->my_lock));
        return false;
    }
    else {
        *t = self->head_ptr;
        self->head_ptr = self->head_ptr->next;
        my_mutex_unlock(&(self->my_lock));
        return true;
    }

    #else

    my_mutex_lock(&(self->my_lock));
    struct task* old_top = self->task_pool;
    if( old_top == NULL ) {
        my_mutex_unlock(&(self->my_lock));
        return  false;
    }
    else{
       *t = old_top; //3r1w
       self->task_pool = old_top->next;
       my_mutex_unlock(&(self->my_lock));
       return true;
    }  
    #endif
#else   
    struct task* old_top = self->task_pool;
    if( old_top == NULL )
    {
       return  false;
    }else{
       struct task* new_top = old_top->next;
       if(_sync_val_compare_and_swap(&(self->my_lock), &(self->task_pool),old_top,new_top)==old_top)
       {
          *t = old_top; 
          return true;
       }else{
          *t=NULL;
          return false;
       }
    } 
#endif    
}
  #endif
#endif






/*

#ifdef _CIRCULAR_QUEUE
#ifdef MASTER
void push(struct concurrent_aceMesh_stack* self,struct task* t)    
{
#ifdef mutex_lock
    struct task* old_top=NULL;
    my_mutex_lock(&(self->my_lock));
    old_top = self->task_pool;
    t->next=old_top;
    self->task_pool=t;
    my_mutex_unlock(&(self->my_lock));
#else
    struct task* old_top=NULL;
    do{
      old_top = self->task_pool;
      t->next = old_top;
    } while(_sync_val_compare_and_swap(&(self->my_lock),
                     &(self->task_pool),
                      old_top,t)!=old_top);
#endif
}
int try_pop(struct concurrent_aceMesh_stack* self,struct task** t)
{
#ifdef mutex_lock    
    struct task* old_top =NULL;
    if(self->task_pool==NULL)
        return false;
    else
    {
        my_mutex_lock(&(self->my_lock));
        struct task* new_top=NULL;
        old_top = self->task_pool;
        new_top = old_top->next;
        
        *t=old_top;
        self->task_pool=new_top;
        my_mutex_unlock(&(self->my_lock));
        if((*t)==NULL)
        {
            return false;
        }
        else
        {
            return true;
       }
    } 
#else   
    struct task* old_top = self->task_pool;
    if( old_top == NULL )
    {
       return  false;
    }else{
       struct task* new_top = old_top->next;
       if(_sync_val_compare_and_swap(&(self->my_lock), &(self->task_pool),old_top,new_top)==old_top)
       {
          *t = old_top; 
          return true;
       }else{
          *t=NULL;
          return false;
       }
    } 
#endif    
}
void push_to_slave(struct aceMesh_stack* self,struct  task* t)
{		
    struct task** base=self->base;
	int curtop=self->top;
    base[curtop]=t;
    //printf("push %d to%d,",t->affinity_id,curtop);
    curtop=(curtop+1)%CIRCULAR_ARRAY_SIZE;
    if (curtop==self->bottom) {
      printf("sorry: one mqueue(circular) is full!\n"); //actually is safe currently
	  exit(1);
	}
    self->top=curtop;
}
#else
void push(struct aceMesh_stack* self,struct  task* t)
{		
    struct task** base=self->base;
    int curtop=self->top;
    base[curtop]=t;
    //printf("push %d to%d,",t->affinity_id,curtop);
    curtop=(curtop+1)%CIRCULAR_ARRAY_SIZE;
    if (curtop==self->bottom) {
      printf("sorry: one mqueue(circular) is full!\n"); //actually is safe currently
      exit(1);
    }
    self->top=curtop;
}
#endif
#elif defined( _SERIAL_QUEUE)
#ifdef MASTER
void push(struct concurrent_aceMesh_stack* self,struct task* t)    
{            
    struct task* old_top=NULL;
    do{
      old_top = self->task_pool;
      t->next = old_top;
    } while(_sync_val_compare_and_swap(&(self->my_lock),
                     &(self->task_pool),
                      old_top,t)!=old_top);
}
int try_pop(struct concurrent_aceMesh_stack* self,struct task** t)
{
    struct task* old_top = self->task_pool;
    if( old_top == NULL )
    {
       return  false;
    }else{
       struct task* new_top = old_top->next;
       if(_sync_val_compare_and_swap(&(self->my_lock), &(self->task_pool),old_top,new_top)==old_top)
       {
          *t = old_top; 
          return true;
       }else{
          *t=NULL;
          return false;
       }
    } 
}
void push_to_slave(struct aceMesh_stack* self,struct  task* t)
{
    struct task* old_top=NULL;
    old_top = self->task_pool;
    t->next = old_top;
    self->task_pool=t;
}
#else
void push(struct aceMesh_stack* self,struct  task* t)
{
    struct task* old_top=NULL;
    old_top = self->task_pool;
    t->next = old_top;
    self->task_pool=t;
}
#endif

#else
#ifndef MASTER
void push(struct concurrent_aceMesh_stack* self,struct  task* t)
{
    struct task* old_top=NULL;
    old_top = self->task_pool;
    t->next = old_top;
    self->task_pool=t;
}


int try_push(struct concurrent_aceMesh_stack* self,struct task* t)
{
    struct task* old_top = self->task_pool;
    t->next = old_top;
    self->task_pool=t; 
    return true;
}

#else
	

#ifdef SEG_BUFF
//如何获取队列的指针，目前这种方法必须每个队列配置一套主存变量
//环形链表和普通链表差别在于主核的访存效率，这里先用链表实现

//int mqueue_top;
//int mqueue_bottom;
#ifdef ARRAY_STACK
void copy_task_queue(struct aceMesh_stack* src, struct aceMesh_stack* dest)
{
    int s_top=src->top;
    int d_top=dest->top;
    int size=s_top;
    if(size>0)
    {
        if(d_top+size>=PENDING_BUFF_SIZE)
        {
            printf("dest_queue is full");
            exit(1);
        }
        memcpy(&(src->base), &(dest->base[d_top]),sizeof(struct task*)*size);
        //printf("copy_size=%d\n",sizeof(struct task*)*size);
        src->top=0;
        dest->top=d_top+size;
        //printf("dest_queue_size=%d\n",dest->top);
    }
}

void ser_push(struct aceMesh_stack* self,struct  task* t)
{
        struct task** base=self->base;
        int cur_top=self->top;
        //printf("cur_top=%d\n",cur_top);
        base[cur_top]=t;
        cur_top=cur_top+1;
        if(cur_top== PENDING_BUFF_SIZE-1)
        {
            printf("push mqueue is full\n");
            exit(1);
        }
        self->top=cur_top;
        //printf("cur_top=%d\n",cur_top);
}


int ser_try_pop(struct aceMesh_stack* self,struct task** t)
{
        struct task** base=self->base;
        int cur_top=self->top;
        if(cur_top==0) 
            return false;
        else
        {
            //printf("before_get_a_task=%p",*t);
            *t=base[--cur_top];
            self->top=cur_top;
            //printf("get_a_task=%p\n",*t);
            return true;
        }
}

#endif

void push(struct concurrent_aceMesh_stack* self,struct  task* t)
{
    struct task* old_top=NULL;
    old_top = self->task_pool;
    t->next = old_top;
    self->task_pool=t;
}


int try_pop(struct concurrent_aceMesh_stack* self,struct task** t)
{
    //TODO: can be opt! less dereference
    struct task* old_top = self->task_pool;
    if( old_top == NULL )
       return  false;
    else{
       *t = old_top;
       self->task_pool = old_top->next;
       return true;
   }  
}


#else

#ifdef TARG_SW5
void push(struct concurrent_aceMesh_stack* self,struct task* t)    
{            
    struct task* old_top=NULL;
    do{
      old_top = self->task_pool;
      t->next = old_top;
    } while(_sync_val_compare_and_swap(&(self->my_lock),
                     &(self->task_pool),
                      old_top,t)!=old_top);
}

int try_pop(struct concurrent_aceMesh_stack* self,struct task** t)
{
    struct task* old_top = self->task_pool;
    if( old_top == NULL )
    {
       return  false;
    }else{
       struct task* new_top = old_top->next;
       if(_sync_val_compare_and_swap(&(self->my_lock), &(self->task_pool),old_top,new_top)==old_top)
       {
          *t = old_top; 
          return true;
       }else{
          *t=NULL;
          return false;
       }
    } 
}

#elif TARG_SW9
void push(struct concurrent_aceMesh_stack* self,struct task* t)    
{    
    struct task* old_top=NULL;
    do{ 
      old_top = self->task_pool;
      t->next = old_top;
    } while(!__sync_bool_compare_and_swap(&(self->task_pool), old_top,t));
    printf("q_push ");
    fflush(stdout);
}
int try_pop(struct concurrent_aceMesh_stack* self,struct task** t)
{
    struct task* old_top = self->task_pool;
    if( old_top == NULL )
    {   
       return  false;
    }else{
       struct task* new_top = old_top->next;
       if(__sync_bool_compare_and_swap(&(self->task_pool),old_top,new_top))
       {
          *t = old_top; 
          return true;
       }else{
          *t=NULL;
          return false;
       }
    }   
}

#endif
//end of TARG
//#endif 
//end of SEG_BUFF


//#endif
//end of MASTER
//#endif
//end of queue

*/



#ifdef MASTER
#ifdef MPI_SURPPORT
void q_push(struct aceMesh_queue* self ,struct task* t)
{
    t->next=NULL;
    if(self->head==NULL)
    {
         self->head=t;
         self->tail=t;
         return;
    }
    self->tail->next=t;
    //printf("q_push \n");
    self->tail=t;
}
int q_try_pop(struct aceMesh_queue* self, struct task** t)
{
    struct task* old_top=self->head;
    if(old_top==NULL)
        return 0;
    else{
        *t=old_top;
        self->head=old_top->next;
        return 1;
    }
}
#ifdef MTEST_LIGHT
void q_push_mtest(struct mtest_queue* self ,struct mtest_task* t)
{
   //printf("q_push_mtest\n");
    t->next=NULL;
    if(self->head==NULL)
    {
         self->head=t;
         self->tail=t;
         return;
    }
    self->tail->next=t;
    //printf("q_push \n");
    self->tail=t;
}
int q_try_pop_test(struct mtest_queue* self, struct mtest_task** t)
{
    struct mtest_task* old_top=self->head;
    if(old_top==NULL)
        return 0;
    else{
        *t=old_top;
        self->head=old_top->next;
        return 1;
    }
}
#endif
#endif
#endif



